<?php
class ControllerDelimanPhieuphanphoi extends Controller
{
	private $error = array();
   	function __construct() 
	{
		$listchek = $this->getListCheck();
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
		$this->data['list_button'] = array();
		foreach($listchek as $method)
		{
			$this->data['list_button'][$method] = $this->user->hasPermission($this->getRoute(), $method);
		}
		
	 	$this->load->model("deliman/phieuphanphoi");
		$this->load->model("deliman/nhanvien");
		$this->load->model("deliman/khuvuc");
		$this->load->model("deliman/loaibao");
		$this->load->model("deliman/kybao");
		$this->load->model("common/control");
		$this->data['loaibaos'] = $this->model_deliman_loaibao->getList();
		$where = " AND loainhanvienid = 1 OR loainhanvienid = 4";
		$this->data['nhanviens'] = $this->model_deliman_nhanvien->getList($where);
   	}
	
	public function index()
	{
		$this->getList();
	}
	
	public function insert()
	{
    	$this->getForm();
	}
	
	public function update()
	{
		$this->data['haspass'] = false;
		$this->data['readonly'] = 'readonly="readonly"';
		$this->data['class'] = 'readonly';	
		$this->getForm();
  	}
	//Tac nhan chinh la nhan vien quan ly khu vuc
	public function phanphoibao()
	{
		$ctphanphoi = array();
		if ((isset($this->request->get['phieuphanphoiid'])) ) 
		{
      		$this->data['item'] = $this->model_deliman_phieuphanphoi->getItem($this->request->get['phieuphanphoiid']);
			$where = " AND phieuphanphoiid = '". $this->request->get['phieuphanphoiid'] ."'";
			$ctphanphoi = $this->model_deliman_phieuphanphoi->getChiTietPhieuPhanPhoiList($where);
    	}
		
		//Lay cac khu vuc ma nhan vien do quan ly
		$where = " AND nhanvienphutrachid = '". $this->user->getNhanVien('nhanvienid') ."'";
		$khuvucql = $this->model_deliman_khuvuc->getList($where);
		$this->data['khuvucquanlys'] = $khuvucql;
		
		
		
		
		$this->id='content';
		$this->template='deliman/phieuphanphoi_phanphoibao.tpl';
		$this->layout="layout/deliman";
		$this->render();
	}
	
	public function loadKhuVucPhanPhoi()
	{
		$khuvucid = $this->request->get['khuvucid'];
		$phieuphanphoiid = $this->request->get['phieuphanphoiid'];
		
		$this->data['item'] = $this->model_deliman_phieuphanphoi->getItem($phieuphanphoiid);
		$where = " AND phieuphanphoiid = '". $phieuphanphoiid ."'";
		$ctphanphoi = $this->model_deliman_phieuphanphoi->getChiTietPhieuPhanPhoiList($where);
		
		$row = $this->string->array_Filter($ctphanphoi,'khuvucid',$khuvucid);
		$this->data['chitieukhuvuc'] = $row[0];
		
		$this->data['khuvucs'] = array();
		$arrKhuVucTree = array();
		
		$this->model_deliman_khuvuc->getTree($khuvucid,$arrKhuVucTree,$khuvucid);
		
		$eid="ex-node";
		$eclass="child-of-ex-node";
		
		foreach($arrKhuVucTree as $item )
		{	
			
			$deep = $item['level'];	
			$tab="";
			
			if(count($item['countchild'])==0)
				 $tab="<span class='tab'></span>";
		
			$class="";
			
			if($item['parentpath']!="")
			{
				$class=$eclass.$item['parentpath'];
			}
			$row = $this->string->array_Filter($ctphanphoi,'khuvucid',$item['khuvucid']);
			$disabled = "";
			//print_r($row);
			if($row[0]['qlkhuvucid'] == $this->user->getNhanVien())
			{
				$disabled ='disabled';	
			}
			$this->data["khuvucs"][]=array(
										
										'ctphanphoi' =>$row[0],
										'khuvucid'=>$item['khuvucid'],
										'disabled' => $disabled,
										'prefix'=>$this->string->getPrefix("--",$deep),
										'deep'=>$deep + 1,
										'eid' =>$eid . $item['path'] ,
										'class' =>$class,
										'tenkhuvuc'=>$item['tenkhuvuc'],
										'khuvuccha'=>$item['khuvuccha'],
										'position'=>$item['position'],
										'tab'=>$tab
										
								    );
		
		}
		$this->data["khuvucs"][0]['disabled'] = 'disabled';
		$this->id='content';
		$this->template='deliman/phieuphanphoi_khuvuc_phanphoibao.tpl';
		
		$this->render();
	}
	
	public function delete() 
	{
		$listphieuphanphoiid=$this->request->post['delete'];
		
		if(count($listphieuphanphoiid))
		{
			foreach($listphieuphanphoiid as $phieuphanphoiid)
			{
				$this->model_deliman_phieuphanphoi->delete($phieuphanphoiid);	
			}
			$this->data['output'] = "true";
		}
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	private function getList() 
	{
		$this->data['insert'] = $this->url->http('deliman/phieuphanphoi/insert');
		$this->data['delete'] = $this->url->http('deliman/phieuphanphoi/delete');		
		
		
		
		$this->data['datas'] = array();
		$where = "";
		$data = $this->request->get;
		
		if (trim($data['tungay']) != "")
		{
			$where .= " AND ngayphathanh >= '".$this->date->formatViewDate($data['tungay'])."'";	
			
		}
		if (trim($data['denngay']) != "")
		{
			$where .= " AND ngayphathanh < '".$this->date->formatViewDate($data['denngay'])." 24:00:00'";	
		}		
		
		if($data['loaibaoid'] != "")
		{
			$where .= " AND loaibaoid = '".$data['loaibaoid']."'";
		}
		if($data['kybaoid'] != "")
		{
			$where .= " AND kybaoid = '".$data['kybaoid']."'";
		}
		if($data['nhanvienlapid'] != "")
		{
			$where .= " AND nhanvienlapid = '".$data['nhanvienlapid']."'";
		}
		$where .= " Order by ngayphathanh DESC";
		$rows = $this->model_deliman_phieuphanphoi->getList($where);
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		//for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		for($i=0;$i < count($rows[$i]);$i++)
		{
			$this->data['datas'][$i] = $rows[$i];
			$this->data['datas'][$i]['link_edit'] = $this->url->http('deliman/phieuphanphoi/update&phieuphanphoiid='.$this->data['datas'][$i]['phieuphanphoiid']);
			$this->data['datas'][$i]['text_edit'] = "Edit";
			
			$this->data['datas'][$i]['link_phanphoibao'] = $this->url->http('deliman/phieuphanphoi/phanphoibao&phieuphanphoiid='.$this->data['datas'][$i]['phieuphanphoiid']);
			$this->data['datas'][$i]['text_phanphoibao'] = "Phân phối báo";
			
		}
		//Tao group theo loai bao
		$arr_loaibaoid = $this->string->matrixToArray($this->data['datas'],'loaibaoid');
		$this->load->model('deliman/loaibao');
		$where = " AND loaibaoid in ('".implode("','",$arr_loaibaoid)."')";
		$this->data['data_loaibao'] = $this->model_deliman_loaibao->getList($where);
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="deliman/phieuphanphoi_list.tpl";
		$this->layout="layout/deliman";
		
		$this->render();
	}
	
	
	private function getForm()
	{		
		$ctphanphoi = array();
		if ((isset($this->request->get['phieuphanphoiid'])) ) 
		{
      		$this->data['item'] = $this->model_deliman_phieuphanphoi->getItem($this->request->get['phieuphanphoiid']);
			$where = " AND phieuphanphoiid = '". $this->request->get['phieuphanphoiid'] ."'";
			$ctphanphoi = $this->model_deliman_phieuphanphoi->getChiTietPhieuPhanPhoiList($where);
    	}
		$this->data['khuvucs'] = array();
		$arrKhuVucTree = array();
		$this->model_deliman_khuvuc->getTree(0,$arrKhuVucTree);
		
		$eid="ex-node";
		$eclass="child-of-ex-node";
		
		foreach($arrKhuVucTree as $item )
		{	
			
			$deep = $item['level'];	
			$tab="";
			
			if(count($item['countchild'])==0)
				 $tab="<span class='tab'></span>";
		
			$class="";
			
			if($item['parentpath']!="")
				$class=$eclass.$item['parentpath'];
			
			$row = $this->string->array_Filter($ctphanphoi,'khuvucid',$item['khuvucid']);
			
			$this->data["khuvucs"][]=array(
										
										'ctphanphoi' =>$row[0],
										'khuvucid'=>$item['khuvucid'],
										'prefix'=>$this->string->getPrefix("--",$deep),
										'deep'=>$deep + 1,
										'eid' =>$eid . $item['path'] ,
										'class' =>$class,
										'tenkhuvuc'=>$item['tenkhuvuc'],
										'khuvuccha'=>$item['khuvuccha'],
										'position'=>$item['position'],
										'tab'=>$tab
										
								    );
		
		}
		
		$this->id='content';
		$this->template='deliman/phieuphanphoi_form.tpl';
		$this->layout="layout/deliman";
		$this->render();
	}
	
	public function savephanphoikhuvuc()
	{
		$data = $this->request->post;
		//Luu chi tien cho tung khu vuc
		$chitieukv = $data['chitieukv'];
		print_r($chitieukvs);
		foreach($chitieukv as $key => $val)
		{
			
			//if( (int)$val!="0")
			{
				$ctphanphoi['id'] = $data['id'][$key];
				$ctphanphoi['phieuphanphoiid'] = $data['phieuphanphoiid'];
				$ctphanphoi['loaibaoid'] = $data['loaibaoid'];
				$ctphanphoi['kybaoid'] = $data['kybaoid'];
				$ctphanphoi['khuvucid'] = $key;
				$khuvuc = $this->model_deliman_khuvuc->getItem($ctphanphoi['khuvucid']);
				$ctphanphoi['tenkhuvuc'] = $khuvuc['tenkhuvuc'];
				
				$nhanvien = $this->model_deliman_nhanvien->getItem($khuvuc['nhanvienphutrachid']);
				$ctphanphoi['qlkhuvucid'] = $nhanvien['nhanvienid'];
				$ctphanphoi['masoqlkhuvuc'] = $nhanvien['masonhanvien'];
				$ctphanphoi['tenqlkhuvuc'] = $nhanvien['hoten'];
				$ctphanphoi['chitieu'] = $val;
				
				$this->model_deliman_phieuphanphoi->saveChiTietPhieuPhanPhoi($ctphanphoi);
				
			}
		}
		
		$this->data['output'] = "true";
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	public function save()
	{
		$data = $this->request->post;
		
		if($this->validateForm($data))
		{
			$mon = $this->date->numberFormate($this->date->now['mon']);
			$year = $this->date->now['year'];
			$prefix = $mon.$year;
			$data['prefix'] ="PPB".$prefix;
			$kybao = $this->model_deliman_kybao->getItem($data['kybaoid']);
			$data['sokybao'] = $kybao['sokybao'];
			$data['tenkybao'] = $kybao['tenkybao'];
			$loaibao = $this->model_deliman_loaibao->getItem($data['loaibaoid']);
			$data['tenloaibao'] = $loaibao['tenloaibao'];
			$data['ngayphathanh'] = $kybao['ngayphathanh'];
			
			if($data['phieuphanphoiid']=="")
			{
				$data['phieuphanphoiid'] = $this->model_deliman_phieuphanphoi->insert($data);	
			}
			else
			{
				$this->model_deliman_phieuphanphoi->update($data);	
			}
			//Luu chi tien cho tung khu vuc
			$chitieukv = $data['chitieukv'];
			
			foreach($chitieukv as $key => $val)
			{
				
				if($val!="0" || (int)$data['id'][$key] !=0)
				{
					$ctphanphoi['id'] = $data['id'][$key];
					$ctphanphoi['phieuphanphoiid'] = $data['phieuphanphoiid'];
					$ctphanphoi['loaibaoid'] = $data['loaibaoid'];
					$ctphanphoi['kybaoid'] = $data['kybaoid'];
					$ctphanphoi['khuvucid'] = $key;
					$khuvuc = $this->model_deliman_khuvuc->getItem($ctphanphoi['khuvucid']);
					$ctphanphoi['tenkhuvuc'] = $khuvuc['tenkhuvuc'];
					
					$nhanvien = $this->model_deliman_nhanvien->getItem($khuvuc['nhanvienphutrachid']);
					$ctphanphoi['qlkhuvucid'] = $nhanvien['nhanvienid'];
					$ctphanphoi['masoqlkhuvuc'] = $nhanvien['masonhanvien'];
					$ctphanphoi['tenqlkhuvuc'] = $nhanvien['hoten'];
					$ctphanphoi['chitieu'] = $val;
					$ctphanphoi['ngayphanphoi'] = $data['ngayphanphoi'];
					$ctphanphoi['ngayphathanh'] = $data['ngayphathanh'];
					
					$this->model_deliman_phieuphanphoi->saveChiTietPhieuPhanPhoi($ctphanphoi);
					
				}
			}
			
			$this->data['output'] = "true";
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item."<br>";
			}
		}
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateForm($data)
	{
		/*if (trim($data['maphieuphanphoi']) == "") 
		{
      		$this->error['maphieuphanphoi'] = "Bạn chưa nhập mã chức năng";
    	}
		else
		{
			$where = " AND maphieuphanphoi = '".$data['maphieuphanphoi']."'";
			$kybaos = $this->model_deliman_phieuphanphoi->getList($where);
			if(count($kybaos) > 0 && $data['phieuphanphoiid'] == "")
			{
				$this->error['maphieuphanphoi'] = "Mã chức năng đã tồn tại trong hệ thống";	
			}
		}*/
		if (trim($data['kybaoid']) == "") 
		{
      		$this->error['kybaoid'] = "Bạn chưa chọn kỳ báo";
    	}
		if (trim($data['chitieu']) == 0) 
		{
      		$this->error['chitieu'] = "Bạn chưa nhập chỉ tiêu";
    	}
	
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	//Cac ham xu ly tren form
	public function getKyBaos()
	{
		$loaibaoid = $this->request->get['loaibaoid'];
		$kybaoid = $this->request->get['kybaoid'];
		//Lay ds cac ky bao da phan phoi
		$where = " AND kybaoid <> '".$kybaoid."'";
		$data_phieuphanphois = $this->model_deliman_phieuphanphoi->getList($where);
		$arr_kybaoid = $this->string->matrixToArray($data_phieuphanphois,'kybaoid');
		
		//
		$where = " AND loaibaoid = '".$loaibaoid."'";
		$where .= " AND kybaoid not in ('". implode("','",$arr_kybaoid) ."')";
		$kybaos = $this->model_deliman_kybao->getList($where." ORDER BY ngayphathanh DESC");
		$cb = "<option value=''>Chọn kỳ báo</option>";
		$cb .= $this->model_common_control->getDataCombobox($kybaos, 'tenkybao', 'kybaoid');
		$this->data['output'] = $cb;
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
}
?>